<template>
  <div>
    <el-card>
      <div slot="header" class="clearfix">
        <span>{{title}}</span>
      </div>
      <el-form ref="addForm" :model="addForm" :rules="rules" label-position="left" label-width="120px" class="infos flex">
        <el-form-item label="产品族" prop="cateName" style="width:30%">
          <el-input v-model="addForm.cateName" placeholder="请输入产品族"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort" style="width:30%">
          <el-input v-model="addForm.sort" placeholder="请输入排序"></el-input>
        </el-form-item>
        <el-form-item label="是否推荐" prop="isShow" style="width:30%">
          <el-radio v-model="addForm.isShow" label="1">推荐</el-radio>
  				<el-radio v-model="addForm.isShow" label="2">不推荐</el-radio>
        </el-form-item>
      </el-form>
      <el-button size="large" type="primary" style="width:30%;display:block;margin:0 auto" @click="submitForm('addForm')">提交</el-button>
    </el-card>
  </div>
</template>

<script>
import { CATEGORY_UPDATE } from "@/utils/api";
export default {
  data() {
    return {
      title: "修改物料分类",
      addForm: {
        id: '',
      	pid:'',
        cateName: '',
        sort: '',
        isShow:'',
        pic:''
      },
      rules: {
          cateName: [
            { required: true, message: '请输入产品族', trigger: 'blur' },
          ],
          sort: [
            { required: true, message: '请输入排序', trigger: 'blur' }
          ],
          isShow: [
            { required: true, message: '请选择是否推荐', trigger: 'blur' }
          ]
        }
    };
  },
  mounted() {
    // 参数获取
    const defaultSupplier = this.$route.query["data"]
    if (!defaultSupplier) {
      this.$message.success('系统异常')
      this.$router.push({ path: "Suppliers" })
    }
    // 默认赋值
    this.addForm.id = defaultSupplier.id
    this.addForm.pid = defaultSupplier.pid
    this.addForm.cateName = defaultSupplier.cateName
    this.addForm.sort = defaultSupplier.sort
    this.addForm.isShow = defaultSupplier.isShow
    this.addForm.pic = defaultSupplier.pic
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addSupplier()
        } else {
          alert('error submit!');
          return false;
        }
      });
    },
    addSupplier(){
      CATEGORY_UPDATE({
        id: this.addForm.id,
        pid:this.addForm.pid,
        cateName: this.addForm.cateName,
        sort:this.addForm.sort,
        isShow:this.addForm.isShow,
        pic:this.addForm.pic
      }).then(res => {
        this.$message.success(JSON.stringify(res))  
        if(res.status==200){
          this.$message.success('success')  
          this.$router.push({ path: "Materials" })
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.infos {
  width: 100%;
  justify-content: space-between;
  flex-wrap: wrap;
  .item {
    width: 30%;
  }
}
</style>